package com.ouran.controller.user;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ouran.common.Util;
import com.ouran.model.page.PageConstants;
import com.ouran.model.page.Pager;
import com.ouran.model.user.User;
import com.ouran.service.user.UserService;

/**
 * 用户模块Controller
 * 
 * @author lijiajun
 * @version 1.0 Date 2018/07/28
 */
@Controller
@RequestMapping("/user")
public class UserController {

	Logger logger = Logger.getLogger(UserController.class);

	/**
	 * 引用用户模块Service
	 */
	@Resource
	UserService userServiceImpl;

	@Autowired
	HttpServletRequest request;

	/**
	 * 测试redis
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/testredis", method = RequestMethod.GET)
	public void testredis() throws Exception {
//		User user = new User();
//		user.setId(1);
//		user.setUserName("lijiajun");
//		user.setPassWord("password");
//		userServiceImpl.redisSaveUser(user);
//		System.out.println("redisSaveUser: " + user.toString());
//		System.out.println("redisGetUser: " + userServiceImpl.redisGetUser(1).toString());

	}

	/**
	 * 访问登陆页面
	 * 
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() throws Exception {

		return "user/login";
	}

	/**
	 * 用户退出
	 * 
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/logout", method = RequestMethod.GET)
	public String login(HttpSession session) throws Exception {

		// 删除用户session
		session.removeAttribute("user");
		return "user/login";
	}

	// responseBody将返回值转化为json格式响应到客户端
	// requestBody将请求数据转化为json对象
	@RequestMapping(value = "/verifyCode", method = RequestMethod.POST)
	public @ResponseBody boolean verifyCode(String verifyCode) throws Exception {

		return Util.verifyKaptchaValue(request, verifyCode);
	}

	/**
	 * 用户登陆校验
	 * 
	 * @param userName
	 *            用户名
	 * @param passWord
	 *            密码
	 * @param session
	 *            会话
	 * @param model
	 *            数据对象
	 * @return 页面地址：首页or调回登录页
	 * @throws Exception
	 */
	@RequestMapping(value = "/login", method = RequestMethod.POST)
	public String login(@RequestParam("userName") String userName, @RequestParam("passWord") String passWord,
			HttpSession session, Model model) throws Exception {

		logger.info("UserController.login 用户登陆 start  userName： " + userName);
		boolean flag = false;
		List<User> list = userServiceImpl.checkLogin(userName, passWord);
		// 没有查询到用户信息登录失败
		if (list.isEmpty()) {
			flag = true;
			session.setAttribute("flag", true);
			session.setAttribute("errmsg", "用户名或密码错误，请重新输入！");

			logger.info("UserController.login 用户登陆  userName： " + userName + " 用户名或密码错误，请重新输入！");
		} else {
			session.setAttribute("user", (User)list.get(0));
		}

		logger.info("UserController.login 用户登陆 end  userName： " + userName);

		return !flag ? "redirect:/information/releas" : "redirect:/user/login";
	}

	/**
	 * 打开新增用户页面
	 * 
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public String userAdd() throws Exception {
		return "user/userAdd";
	}

	/**
	 * 新增用户
	 * 
	 * @param user
	 *            用户实体
	 * @return 页面地址
	 */
	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public String userAdd(@ModelAttribute("user") User user) throws Exception {
		userServiceImpl.addUser(user);
		return "redirect:login";
	}

	/**
	 * 打开用户管理页面
	 * 
	 * @param model
	 *            数据对象
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/userManage", method = RequestMethod.GET)
	public String userManage(Model model) throws Exception {

		Pager<User> pager = new Pager<User>();
		pager.setOffset(PageConstants.getOffset());
		pager.setSize(PageConstants.getSize());
		model.addAttribute("pagers", userServiceImpl.userList(pager));
		return "user/userManage";
	}

	/**
	 * 显示用户详情页
	 * 
	 * @param id
	 *            用户id
	 * @param model
	 *            数据对象
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/userShow/{id}", method = RequestMethod.GET)
	public String userShow(@PathVariable int id, Model model) throws Exception {
		User user = userServiceImpl.showUser(id);
		model.addAttribute(user);
		return "user/userShow";
	}

	/**
	 * 查询用户，返回结果
	 * 
	 * @param userName
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryUserByName", method = RequestMethod.POST)
	public @ResponseBody boolean queryUserByName(String userName) throws Exception {
		if (userServiceImpl.showUser(userName)) {
			return true;
		}
		return false;
	}
	/**
	 * 显示修改用户页面
	 * 
	 * @param id
	 *            用户id
	 * @param model
	 *            数据对象
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/userUpdate/{id}", method = RequestMethod.GET)
	public String userUpdate(@PathVariable int id, Model model) throws Exception {
		User user = userServiceImpl.showUser(id);
		model.addAttribute(user);
		return "user/userUpdate";

	}

	/**
	 * 修改用户
	 * 
	 * @param user
	 *            用户对象
	 * @param model
	 *            数据对象
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/userUpdate", method = RequestMethod.POST)
	public String userUpdate(@ModelAttribute("user") User user, Model model) throws Exception {
		userServiceImpl.updateUser(user);
		return "redirect:userUpdate/" + user.getId();

	}

	/**
	 * 删除用户信息
	 * 
	 * @param id
	 *            用户id
	 * @throws Exception
	 * @return 页面地址
	 */
	@RequestMapping(value = "/userDelete/{id}", method = RequestMethod.GET)
	public String userDelete(@PathVariable int id) throws Exception {
		userServiceImpl.deleteUser(id);
		return "redirect:/user/userManage";
	}
}
